﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace The_LarX
{
    public class LarPropertyGrid : Component
    {
        protected object selectedObject;
        public object SelectedObject
        {
            get { return selectedObject; }
            set
            {
                if (value != selectedObject)
                {
                    selectedObject = value;
                    GenerateProperties();
                }
            }
        }
                
        public Color BackgroundColor { get; set; }

        public Rectangle OptionsRectangle { get; protected set; }

        public Camera2D Camera;

        public List<LarGridPropertyHandler> Properties;

        public int X;

        public LarTrackBar ScrollBar;

        public LarPropertyGrid(GameScreen Screen)
            : base(Screen)
        {
            this.BackgroundColor = new Color(128, 128, 128, 100);
            this.Camera = new Camera2D(Screen);
            this.Width = 600;
            this.Serialiable = false;

            this.ScrollBar = new LarTrackBar(10, 600, 1280 - 40, 40, 32, 32, 0, 0);
            this.ScrollBar.CurrentValue = 0;
            this.ScrollBar.OnSlide += new EventHandler(ScrollBar_OnSlide);

            this.Properties = new List<LarGridPropertyHandler>();

            X = 1280 - this.Width;

            this.OptionsRectangle = new Rectangle(
                X - (Width / 10),
                0, Width / 10, 150);
        }

        void ScrollBar_OnSlide(object sender, EventArgs e)
        {
            Camera.Position = new Vector3(0, (int)-ScrollBar.CurrentValue, 0);
        }

        public int OffsetBetweenHandlers = 30;

        private int LastHeight = 0;
        public Rectangle GetNextRectangle(int Height)
        {
            Rectangle rectangle = new Rectangle(this.X, LastHeight + OffsetBetweenHandlers,
                (int)(this.Width * 0.9f), Height);

            LastHeight = rectangle.Y + Height + OffsetBetweenHandlers;

            if (LastHeight > Engine.GraphicsDevice.PresentationParameters.BackBufferHeight)
            {
                int HeightDifference = LastHeight -
                    Engine.GraphicsDevice.PresentationParameters.BackBufferHeight;
                ScrollBar.Maximum = HeightDifference;
            }

            return rectangle;
        }

        public int Width { get; protected set; }

        #region Generate Properties
        public void GenerateProperties()
        {
            this.Properties.Clear();

            PropertyInfo[] properties = selectedObject.GetType().GetProperties();

            Type floatType = typeof(float);
            Type intType = typeof(int);
            Type vector3Type = typeof(Vector3);
            
            for (int i = 0; i < properties.Length; i++)
            {
                LarGridPropertyHandler hand = null;
                if (properties[i].PropertyType == floatType)
                {
                    /// We have a float here
                    hand = new LarGrid_Float(Camera, this,
                        properties[i], SelectedObject);
                }
                if (properties[i].PropertyType == intType)
                {
                    /// We have an int here
                    hand = new LarGrid_Int(Camera, this,
                        properties[i], SelectedObject);
                }
                if (properties[i].PropertyType == vector3Type)
                {
                    
                }

                if (hand != null)
                {
                    Properties.Add(hand);
                }
            }
        }
        #endregion

        public LarGridPropertyHandler LastPropertyClicked;

        #region Update & Draw
        public override void Update()
        {
            base.Update();

            for (int i = 0; i < Properties.Count; i++)
            {
                Properties[i].Update();
            }
            ScrollBar.Update();

            if (LastPropertyClicked != null)
            {
                Keys[] lastKeys = Engine.Input.LastKeyboardStates[0].GetPressedKeys();
                Keys[] key = Engine.Input.CurrentKeyboardStates[0].GetPressedKeys();
                for (int i = 0; i < key.Length; i++)
                    if (!lastKeys.Contains(key[i]))
                        LastPropertyClicked.PressedKeyWithThisSelected(key[i]);
            }
        }
        public override void Draw()
        {
            base.Draw();

            Engine.RenderManager.DrawRectangle(BackgroundColor, new Rectangle(
                X, 0, this.Width, 720), null);

            for (int i = 0; i < Properties.Count; i++)
            {
                Properties[i].Draw();
            }

            Engine.RenderManager.DrawRectangle(BackgroundColor, OptionsRectangle, null);
            ScrollBar.Draw();

            Rectangle mouse = Engine.Input.MouseRect(20, Camera);
            Engine.RenderManager.DrawRectangle(Color.Green, mouse, Camera);
        }
        #endregion
    }
}
